package mastermanagment.controller;

import implementor.ProjectImpl;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

import javax.swing.JOptionPane;

import mastermanagment.view.ProjectInfoView;

import dao.ProjectDao;
import dto.Project;

import utility.Constants;
import utility.Validater;

public class ProjectController implements ActionListener{
	private ProjectInfoView view;
	private Project model;
	private ProjectDao pdao;
	private Project[] projs;
	private static int index=0;
	
	public ProjectController() {
		// TODO Auto-generated constructor stub
	}
	
	public ProjectController(ProjectInfoView view, Project model) {
		super();
		this.view = view;
		this.model = model;
		view.actionListener(this);
		pdao=new ProjectImpl();
		try {
			projs=pdao.retreiveAll();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void reactOnSave() throws SQLException, Exception
	{
		String projname=view.getTprojName().getText();
		model=new Project(0, projname,Constants.ACTIVE);
		pdao.add(model);
	}

	private void toggleStatus() throws Exception
	{
		String projname=view.getTprojName().getText();
		
		try
		{
			model=pdao.retreive("projectName", projname);
			if(view.getLblActive().getText().equals("ACTIVE"))
			       model.setProjectStatus(Constants.INACTIVE);
			if(view.getLblActive().getText().equals("INACTIVE"))
				model.setProjectStatus(Constants.ACTIVE);
		}
		catch(Exception e)
		{
			
			e.printStackTrace();
		}
		
		
		pdao.delete(model);
	}
	
	private void reactOnUpdate() throws Exception
	{
		String projname=view.getTprojName().getText();
		
		
		
		model=pdao.retreive("projectName",projname);
		model.setProjectName(projname);
		
		try
		{
			pdao.update(model);
			JOptionPane.showMessageDialog(null, "Data successfully updated");
		}
	
		catch(SQLIntegrityConstraintViolationException e)
		{
			JOptionPane.showMessageDialog(null, "Name already exist, please use another one");
		}
		
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, "Data not successfully added,Please try again !");
		
		}
		// The database is updated now , get all the sellers again
		 this.projs=pdao.retreiveAll();
	}
	
	private void reactOnSearch() throws Exception
	{
		String projname=view.getTprojName().getText();
		
		
	
		
		try
		{
		 model=pdao.retreive("projectName", projname);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		fillViewFormFromModel();
		// search has been done we need to update the index
		for(int i =0;i<projs.length;i++)
			{
			  if(projs[i].equals(projname))
			  {
				  index=i;
			  }
			}
			 
		
		
	}
	

	private void reactOnBack() throws Exception
	{
		if(index>-1)
		{
		model=this.projs[index];
		fillViewFormFromModel();
		index=index-1;
		}
		else
			index=0;
	}
	
   private void reactOnForward() throws Exception
	{
		
	
		if(index<projs.length && index>-1)
		{
			model=this.projs[index];
			fillViewFormFromModel();
			index=index+1;
		}
			
	}
	
	private void fillViewFormFromModel()
	{
		view.getTprojName().setText(model.getProjectName());
		view.getTprojId().setText(model.getAutoIdProject()+"");
		if(model.getProjectStatus() == 1)
		  {
			view.getLblActive().setText("ACTIVE");
			view.getBtnMarkInactive().setText("DEACTIVATE");
		  }
		if(model.getProjectStatus() == 0)
		  {
			view.getLblActive().setText("INACTIVE");
			view.getBtnMarkInactive().setText("ACTIVATE");
		  }
		
	}
	
	@Override
	public void actionPerformed(ActionEvent ae) {
		String k=ae.getActionCommand();
		Validater validates = new Validater(view);
		String message = validates.validateForEmpty();
		if(k.contains("SAVE"))
		{
			
			if(message.equals(""))
			{
			   try {
				reactOnSave();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			   validates.refresh();
			}
			else
				JOptionPane.showMessageDialog(null,message);
		}
		if(k.contains("MARK INACTIVE"))
		{
			try {
				if(message.equals(""))
				{
					toggleStatus();
					validates.refresh();
				}
				else
					JOptionPane.showMessageDialog(null, message);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(k.contains("UPDATE"))
		{
			try {
				if(message.equals(""))
				{
					reactOnUpdate();
					validates.refresh();
				}
				else
					JOptionPane.showMessageDialog(null, message);
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(k.contains("<<"))
		{
			try {
				System.out.println("Back");
				reactOnBack();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(k.contains("SEARCH"))
		{
			try {
				if(message.equals(""))
				{
					reactOnSearch();
					validates.refresh();
				}
				else
					JOptionPane.showMessageDialog(null, message);
				
			
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(k.contains(">>"))
		{
			try {
				reactOnForward();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(k.contains("CLEAR"))
		{
			
		}
		if(k.contains("EXIT "))
		{
			
		}
		
		
	}
	

}
